﻿using System;
using System.Collections.Generic;
using System.Text;
using Model;
using Controls;
using Model.Enum;
using Components;

namespace UI.Lib
{
    public class SearchHelper
    {
        /// <summary>
        /// Manager页面查询后自动得到Where字符串
        /// </summary>
        /// <returns></returns>
        public static string GetSearchWhere(TextValuePair tv, PsDataGridViewTextBoxColumn dc, string value, TextValuePair op, PsDataType tag)
        {
            if (string.IsNullOrEmpty(value)) return "";

            if (tv == null || op == null) return "";

            string s = " 1 = 1 ";
            if (!string.IsNullOrEmpty(value))
            {
                //日期 => 不合法的字符，替换成false条件
                if ((tag == PsDataType.Date || tag == PsDataType.Datetime || tag == PsDataType.Time)
                       && !Validation.IsDate(value))
                {
                    s = " 1 = 0 ";
                }
                else if ((tag == PsDataType.Int || tag == PsDataType.IntThanZero || tag == PsDataType.IntThanZeroOrEqualZero
                    || tag == PsDataType.Num || tag == PsDataType.NumThanZero || tag == PsDataType.NumThanZeroOrEqualZero)
                    && !Validation.IsFloat(value, 10)) //数字 暂时只支持10位的查询
                {
                    s = " 1 = 0 ";
                }
                else
                {
                    s = string.Format(op.Value, value, dc.OtherName);
                }
            }

            string condition = (string.IsNullOrEmpty(value)) ? (string.Format("( {0} OR {1} IS NULL)", s, dc.OtherName))
                                                                : s.ToString();
            return condition;
        }

        //根据字段的类型选择合适的操作符
        public static void GetOperateList(PsDataType tag, System.Windows.Forms.ToolStripComboBox cb)
        {
            if (null == cb) return;
            cb.Items.Clear(); //清空下框

            if (tag == PsDataType.Date || tag == PsDataType.Datetime || tag == PsDataType.Time) //日期
            {
                cb.Items.Add(new TextValuePair("等于", " datediff(d,{1},Convert(datetime,'{0}')) = 0 "));
                cb.Items.Add(new TextValuePair("早于", " {1} > CONVERT(datetime,'{0}')"));
                cb.Items.Add(new TextValuePair("晚于", " {1} < CONVERT(datetime,'{0}')"));
            }
            else if (tag == PsDataType.Int || tag == PsDataType.IntThanZero || tag == PsDataType.IntThanZeroOrEqualZero
                || tag == PsDataType.Num || tag == PsDataType.NumThanZero || tag == PsDataType.NumThanZeroOrEqualZero) //数字
            {
                cb.Items.Add(new TextValuePair("等于", " {1} = CONVERT(numeric(12,2), '{0}')"));
                cb.Items.Add(new TextValuePair("大于", " {1} > CONVERT(numeric(12,2), '{0}')"));
                cb.Items.Add(new TextValuePair("大于或等于", " {1} >= CONVERT(numeric(12,6), '{0}')"));
                cb.Items.Add(new TextValuePair("小于", " {1} < CONVERT(numeric(12,2), '{0}')"));
                cb.Items.Add(new TextValuePair("小于或等于", " {1} <= CONVERT(numeric(12,6), '{0}')"));
                cb.Items.Add(new TextValuePair("不等于", " {1} <> CONVERT(numeric(12,2), '{0}')"));
            }
            else //字符串等其他
            {
                cb.Items.Add(new TextValuePair("包含", " {1} LIKE '%{0}%' "));
                cb.Items.Add(new TextValuePair("不包含", " {1} NOT LIKE '%{0}%'  "));
                cb.Items.Add(new TextValuePair("等于", " {1} = '{0}' "));
                cb.Items.Add(new TextValuePair("以……开头", " {1} LIKE '{0}%' "));
                cb.Items.Add(new TextValuePair("以……结尾", " {1} LIKE '%{0}' "));
            }

            cb.Tag = tag;
            cb.SelectedIndex = 0; //让下拉框默认选中第一个
        }

    }
}
